package org.elip.teststatistics.server;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLStreamException;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.VFS;
import org.elip.teststatistics.client.TestsService;
import org.elip.teststatistics.server.selectors.TestFileSelector;
import org.elip.teststatistics.server.xml.XMLParser;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class TestsServiceImpl extends RemoteServiceServlet implements TestsService {
	
	private Logger logger = Logger.getLogger("logger");
	private FileSystemManager manager;
	private String pathToTestsXap;
	private String pathToTestsCloudify;
	private String pathToTgrid;
	
	@Override
	public void init() throws ServletException{
		try {
			manager = VFS.getManager();
		} catch (FileSystemException e) {
			e.printStackTrace();
			throw new ServletException("Failed getting a FileSystemManager");
		}
		if (File.pathSeparator == "/") {
			pathToTgrid = "/export/tgrid";
			pathToTestsCloudify = pathToTgrid + "/sgtest-cloudify/tmp/test/";
			pathToTestsXap = pathToTgrid + "/sgtest-xap/tmp/test";
		}
		else {
			pathToTgrid = "smb://GSPACES\\ca:password@192.168.10.13/tgrid";
			pathToTestsCloudify = pathToTgrid + "/sgtest-cloudify/tmp/test/";
			pathToTestsXap = pathToTgrid + "/sgtest-xap/tmp/test/";
		}
		super.init();
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String[] generateTests(String product, String suite) {
		
		List<String> temp = new ArrayList<String>();

		String[] packages;
		try {
			packages = XMLParser.parsePackages(product, suite);
			for (String s : packages) {
				try {
					temp.addAll(generateTestsInPackage(product,s));
				} catch (FileSystemException e) {
					logger.info(e.toString());
					return null;
				}
			}
		} catch (XMLStreamException e1) {
			e1.printStackTrace();
			return null;
		} catch (FactoryConfigurationError e1) {
			e1.printStackTrace();
			return null;
		} catch (IOException e1) {
			e1.printStackTrace();
			return null;
		}
		
		String[] result = new String[temp.size()];
		String[] array2 = temp.toArray(result);
		Arrays.sort(array2);
		return array2;
	}
	
	private List<String> generateTestsInPackage(String product,String pck) throws FileSystemException {
		
		List<String> tests = new ArrayList<String>();
		
		FileObject testsFolder = null;
		if (product.equals("XAP")) {			
			testsFolder = manager.resolveFile(pathToTestsXap + pck);
		}
		if (product.equals("Cloudify")) {
			testsFolder = manager.resolveFile(pathToTestsCloudify + pck);
		}
		
		for (FileObject file : testsFolder.findFiles(new TestFileSelector())) {
			tests.add(file.getName().getBaseName().split("\\.")[0]);
		}
		return tests;
		
	}

}
